const db = require('./db')
exports.start = (req, res) => { }
// 登录注册处理
exports.login = (req, res) => {
    let username = req.body.username
    let password = req.body.password
    console.log('*******************');
    console.log(req.body.username);
    console.log(req.body.password);
    console.log('*******************');
    // 查询语句
    let sql = 'select * from student where uname = ?'
    db.base(sql, username, (result, error) => {
        console.log("---------------------");
        console.log(result);
        console.log(result[0].password);
        console.log("---------------------");
        if (!result.length) {
            return res.json({ status: 0, msg: '登录失败' })
        } else {
            if (result[0].password == password) {
                return res.json({ status: 1, msg: '登录成功' })
            }
            return res.json({ status: 1, msg: "密码错误" })
        }
    })
}
exports.register = (req, res) => {
    let username = req.body.username
    let password = req.body.password
    // let id = req.body.id
    // 插入语句
    let sql = 'insert into student (uname,password) values(?,?)'
    try {
        db.base(sql, [username, password], (result, error) => {
            if (error) {
                console.log(error + "12345323")
                return res.json({ status: 0, msg: error.sqlMessage })
            } else {
                return res.json({ status: 1, msg: "注册成功" })
            }
        })
    } catch (err) {
        console.log(err)
    }

}